package com.taptech.doufu.ui.view.choice;

import android.animation.ObjectAnimator;
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.choice.ChoiceTopicBean;
import com.taptech.doufu.bean.choice.TagModel;
import com.taptech.doufu.ui.view.FlowViewGroup;
import com.taptech.doufu.ui.view.readview.util.ScreenUtils;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.weex.http.TFHttpError;
import com.taptech.doufu.weex.http.TFHttpResponse;
import com.taptech.doufu.weex.http.TFHttpUtil;

import java.util.HashMap;
import java.util.List;

public class ChoiceTopicTagView extends LinearLayout {
    private List<TagModel> list;
    private Context context;
    private FlowViewGroup mFlowViewGroup;
    private String replaceUrl;
    private View layReplace;
    private ImageView ivReplace;
    private TextView tvTitle;

    protected EventBean eventData;

    public ChoiceTopicTagView(Context context) {
        super(context);
    }

    public ChoiceTopicTagView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicTagView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ChoiceTopicTagView(Context context, ChoiceTopicBean bean) {
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.topic_choice_tag_view, null);
        mFlowViewGroup = view.findViewById(R.id.mFlowViewGroup);
        view.findViewById(R.id.llMore).setVisibility(GONE);
        tvTitle = view.findViewById(R.id.tvTitle);
        layReplace = view.findViewById(R.id.layReplace);
        ivReplace = view.findViewById(R.id.ivReplace);
        this.context = context;
        tvTitle.setText(bean.getTitle());
        list = bean.getTagList();
        eventData = bean.getEventData();
        replaceUrl = bean.getMoreUrl();
        layReplace.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceData();
            }
        });
        setTags(list);
        addView(view);
    }

    private void setTags(final List<TagModel> list) {
        mFlowViewGroup.removeAllViews();
        mFlowViewGroup.setViewMarginHorizontal(ScreenUtil.dip2px(context,8));
        mFlowViewGroup.setViewMarginVertical(ScreenUtil.dip2px(context,8));
        for (int i = 0; i < list.size(); i++) {
            final TagModel model = list.get(i);
            TextView text = new TextView(context);
            text.setClickable(true);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, ScreenUtils.dpToPxInt(27f));
            text.setLayoutParams(params);
            text.setTextSize(12f);
            text.setText(model.getName());
            text.setGravity(Gravity.CENTER);
            if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                text.setTextColor(ContextCompat.getColor(context,R.color.text_color_47));
                text.setBackgroundResource(R.drawable.bg_tag_gray_dark);
            }else{
                text.setTextColor(ContextCompat.getColor(context,R.color.text_color_6C788A));
                text.setBackgroundResource(R.drawable.bg_tag_gray);
            }
            text.setPadding(ScreenUtils.dpToPxInt(15f), ScreenUtils.dpToPxInt(5f), ScreenUtils.dpToPxInt(15f), ScreenUtils.dpToPxInt(5f));
            text.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChoiceUtils.jumpTagDetail(getContext(),model.getName());
                    ChoiceUtils.addEvent(getContext(),UmengEventName.JINGXUAN_CARD_CONTENT,eventData);
                }
            });
            mFlowViewGroup.addView(text);
        }
    }


    private void replaceData() {
        startAnimator(ivReplace);
        TFHttpUtil.requestGet(replaceUrl, new HashMap(), new TFHttpResponse() {
            @Override
            public void onResponse(JSONObject json, TFHttpError error) {
                if (json != null && json.containsKey("data") &&
                        json.getJSONObject("data")!=null && json.getJSONObject("data").containsKey("list")) {
                    JSONObject jsonObject = json.getJSONObject("data");
                    if (jsonObject.containsKey("moreUrl")) {
                        replaceUrl = jsonObject.getString("moreUrl");
                    }
                    String array = jsonObject.getString("list");
                    List<TagModel> list = JSON.parseArray(array, TagModel.class);
                    if (list.size() > 0) {
                        setTags(list);
                    }else{
                        Toast.makeText(getContext(),"无更多数据",Toast.LENGTH_LONG).show();
                    }
                }
                stopAnimator();
            }
        });
    }

    private ObjectAnimator animator;
    protected void startAnimator(View view) {
        if (animator == null) {
            animator= ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);
            animator.setInterpolator(new LinearInterpolator());
            animator.setRepeatCount(ObjectAnimator.INFINITE);
            animator.setRepeatMode(ObjectAnimator.RESTART);
            animator.setDuration(500);
        }
        animator.start();
    }

    protected void stopAnimator() {
        if (animator != null) {
            animator.setRepeatCount(1);
        }
    }

}
